package com.ddtou.service;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.ddtou.DbUtil;
import com.ddtou.entity.User;

public class UserService {
	
	public UserService() {
		this.db = new DbUtil();
	}
	
	private DbUtil db;

	public DbUtil getDb() {
		return db;
	}

	public void setDb(DbUtil db) {
		this.db = db;
	}
	
	public boolean checkUser(String name){
		boolean b = false;
		String checksql = "select * from myuser where name='"+name.trim()+"'";
		ResultSet selectResult = db.selectSQL(checksql);
		try {
			if(selectResult.next()){
				b=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//db.deconnSQL();
		}
		
		return b;
	}
	
	public Boolean regist(User user){
		String sql = "INSERT INTO \"myuser\"(name, password, createdate) VALUES ( '"+user.getName()+"','"+user.getPassword()+"', current_date);";
		Boolean b = db.insertSQL(sql);
		//db.deconnSQL();
		return b;
	}
	
	
	
	public String login(User user){
		String b = "";
		String sql = "select id from myuser t where t.name='"+user.getName()+"' and password='"+user.getPassword()+"';";
		ResultSet result = this.db.selectSQL(sql);
		try {
			if(result.next()){
				b = result.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//this.db.deconnSQL();
		}
		return b;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
